﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Sgr.BillNumbering;
using Sgr.BillNumbering.Infrastructure;
using Sgr.BillNumbering.Services;
using Sgr.BillNumbering.Services.Impl;
using Sgr.EntityFrameworkCore;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection UseBillNumbering<TContext>(this IServiceCollection services, IConfiguration configuration) where TContext : DbContext
        {
            services.AddOptions<CodeOptions>()
                .BindConfiguration("Sgr:BillNumbering")
                .Validate(opt =>
                {
                    if (opt.DefaultSegmentSize < 1 || opt.DefaultSegmentSize > 100_000)
                    {
                        Console.WriteLine("DefaultSegmentSize 建议范围为 1~100000，当前值：" + opt.DefaultSegmentSize);
                        return false;
                    }
                    if (opt.LazyCleanupThreshold < 1_000 || opt.LazyCleanupThreshold > 10_000_000)
                    {
                        Console.WriteLine("LazyCleanupThreshold 建议范围为 1,000~10,000,000，当前值：" + opt.LazyCleanupThreshold);
                        return false;
                    }
                    if (opt.CleanupMaxRemovePerRound < 1 || opt.CleanupMaxRemovePerRound > 100_000)
                    {
                        Console.WriteLine("CleanupMaxRemovePerRound 建议范围为 1~100,000，当前值：" + opt.CleanupMaxRemovePerRound);
                        return false;
                    }
                    if (opt.CleanupMinIntervalMs < 100 || opt.CleanupMinIntervalMs > 60_000)
                    {
                        Console.WriteLine("CleanupMinIntervalMs 建议范围为 100~60000（毫秒），当前值：" + opt.CleanupMinIntervalMs);
                        return false;
                    }
                    return true;
                }, "编号生成器配置项设置错误：" +
                   "DefaultSegmentSize 建议 1~100000；" +
                   "LazyCleanupThreshold 建议 1,000~10,000,000；" +
                   "CleanupMaxRemovePerRound 建议 1~100,000；" +
                   "CleanupMinIntervalMs 建议 100~60000（毫秒）"
            );

            services.TryAddEnumerable(ServiceDescriptor.Singleton<IEntityFrameworkTypeProvider, SequenceCounterEntityFrameworkTypeProvider>());

            var segmentSize = configuration.GetValue<int>("Sgr:BillNumbering:DefaultSegmentSize");
            if (segmentSize <= 0)
                services.TryAddSingleton<ICodeSequence, DbDirectCodeSequence<TContext>>(); // 注册序列生成器
            else
                services.TryAddSingleton<ICodeSequence, BufferedCodeSequence<TContext>>(); // 注册序列生成器

            services.TryAddEnumerable(ServiceDescriptor.Singleton<ICodeGenerationStrategy, DefaultCodeGenerationStrategy>());// 注册默认策略
            services.TryAddSingleton<ICodeGenerator, DefaultCodeGenerator>(); // 注册编码生成器

            return services;
        }
    }
}